1include<../std.scad>
  2
  3module test_is_region() {
  4    assert(is_region([circle(d=10),square(10)]));
  5    assert(is_region([circle(d=10),square(10),circle(d=50)]));
  6    assert(is_region([square(10)]));
  7    assert(!is_region([]));
  8    assert(!is_region(23));
  9    assert(!is_region(true));
 10    assert(!is_region("foo"));
 11}
 12test_is_region();
 13
 14
 15
 16module test_union() {
 17  R1 = [square(10,center=true), square(9,center=true)];
 18  R2 = [square(9,center=true)];
 19  assert(are_regions_equal(union(R1,R2), [square(10,center=true)]));
 20  assert(are_regions_equal(union(R2,R1), [square(10,center=true)]));
 21  R8 = [right(8,square(10,center=true)), left(8,square(10,center=true))];
 22  R9 = [back(8,square(10,center=true)), fwd(8,square(10,center=true))];
 23  assert(are_regions_equal(union(R9,R8), [[[-5, -5], [-13, -5], [-13, 5], [-5, 5], [-5, 13], [5, 13], [5, 5], [13, 5], [13, -5], [5, -5], [5, -13], [-5, -13]], [[-3, 3], [-3, -3], [3, -3], [3, 3]]]));
 24  assert(are_regions_equal(union(R8,R9), [[[-5, -5], [-13, -5], [-13, 5], [-5, 5], [-5, 13], [5, 13], [5, 5], [13, 5], [13, -5], [5, -5], [5, -13], [-5, -13]], [[-3, 3], [-3, -3], [3, -3], [3, 3]]]));
 25
 26}
 27test_union();
 28
 29
 30module test_intersection() {
 31  R1 = [square(10,center=true), square(9,center=true)];
 32  R6 = [square(9.5,center=true), square(9,center=true)];
 33  assert(are_regions_equal(intersection(R6,R1), R6));
 34  assert(are_regions_equal(intersection(R1,R6), R6));
 35  R8 = [right(8,square(10,center=true)), left(8,square(10,center=true))];
 36  R9 = [back(8,square(10,center=true)), fwd(8,square(10,center=true))];
 37  assert(are_regions_equal(intersection(R9,R8),[[[-3, -5], [-5, -5], [-5, -3], [-3, -3]], [[-5, 5], [-3, 5], [-3, 3], [-5, 3]], [[5, -5], [3, -5], [3, -3], [5, -3]], [[3, 3], [3, 5], [5, 5], [5, 3]]]));
 38  assert(are_regions_equal(intersection(R8,R9),[[[-3, -5], [-5, -5], [-5, -3], [-3, -3]], [[-5, 5], [-3, 5], [-3, 3], [-5, 3]], [[5, -5], [3, -5], [3, -3], [5, -3]], [[3, 3], [3, 5], [5, 5], [5, 3]]]));
 39
 40
 41}
 42test_intersection();
 43
 44
 45module test_difference() {
 46  R5 = [square(10,center=true), square(9,center=true),square(4,center=true)];
 47  R4 = [square(9,center=true), square(3,center=true)];
 48  assert(are_regions_equal(difference(R5,R4),
 49                         [square(10,center=true), square(9, center=true), square(3,center=true)]));
 50  pathA = [
 51    [-9,12], [-6,2], [-3,12], [0,2], [3,10], [5,10], [19,-4], [-8,-4], [-12,0]
 52  ];
 53  pathB = [
 54    [-12,8], [7,8], [9,6], [7,5], [-3,5], [-5,-6], [-2,-6], [0,-4],
 55    [6,-4], [2,-8], [-7,-8], [-15,0]
 56  ];
 57
 58  right=[[[-10, 8], [-9, 12], [-7.8, 8]], [[0, -4], [-4.63636363636, -4], [-3, 5], [-0.9, 5], [0, 2], [1.125, 5], [7, 5], [9, 6], [19, -4], [6, -4]], [[-4.2, 8], [-1.8, 8], [-3, 12]], [[2.25, 8], [3, 10], [5, 10], [7, 8]]];
 59  assert(are_regions_equal(difference(pathA,pathB),right));
 60
 61  R8 = [right(8,square(10,center=true)), left(8,square(10,center=true))];
 62  R9 = [back(8,square(10,center=true)), fwd(8,square(10,center=true))];
 63
 64  assert(are_regions_equal(difference(R9,R8), [[[-5, 5], [-5, 13], [5, 13], [5, 5], [3, 5], [3, 3], [-3, 3], [-3, 5]], [[5, -13], [-5, -13], [-5, -5], [-3, -5], [-3, -3], [3, -3], [3, -5], [5, -5]]]));
 65
 66  assert(are_regions_equal(difference(R8,R9),[[[-5, -5], [-13, -5], [-13, 5], [-5, 5], [-5, 3], [-3, 3], [-3, -3], [-5, -3]], [[3, -3], [3, 3], [5, 3], [5, 5], [13, 5], [13, -5], [5, -5], [5, -3]]]));
 67
 68  
 69}
 70test_difference();
 71
 72
 73
 74module test_exclusive_or() {
 75  R8 = [right(8,square(10,center=true)), left(8,square(10,center=true))];
 76  R9 = [back(8,square(10,center=true)), fwd(8,square(10,center=true))];
 77  assert(are_regions_equal(exclusive_or(R8,R9),[[[-5, -5], [-13, -5], [-13, 5], [-5, 5], [-5, 3], [-3, 3], [-3, -3], [-5, -3]], [[-3, -5], [-5, -5], [-5, -13], [5, -13], [5, -5], [3, -5], [3, -3], [-3, -3]], [[-5, 5], [-3, 5], [-3, 3], [3, 3], [3, 5], [5, 5], [5, 13], [-5, 13]], [[3, -3], [3, 3], [5, 3], [5, 5], [13, 5], [13, -5], [5, -5], [5, -3]]],either_winding=true));
 78  assert(are_regions_equal(exclusive_or(R9,R8),[[[-5, -5], [-13, -5], [-13, 5], [-5, 5], [-5, 3], [-3, 3], [-3, -3], [-5, -3]], [[-3, -5], [-5, -5], [-5, -13], [5, -13], [5, -5], [3, -5], [3, -3], [-3, -3]], [[-5, 5], [-3, 5], [-3, 3], [3, 3], [3, 5], [5, 5], [5, 13], [-5, 13]], [[3, -3], [3, 3], [5, 3], [5, 5], [13, 5], [13, -5], [5, -5], [5, -3]]],either_winding=true));  
 79
 80  p = turtle(["move",100,"left",144], repeat=4);
 81  p2 = move(-centroid(p),p);
 82  p3 = polygon_parts(p2);
 83  p4 = exclusive_or(p3,square(51,center=true));
 84
 85  star_square = [[[-50, -16.2459848116], [-25.5, -16.2459848116],
 86  [-25.5, 1.55430712449]], [[-7.45841874701, 25.5], [-30.9016994375,
 87  42.5325404176], [-25.3674915789, 25.5]], [[-19.0983005625,
 88  6.20541401733], [-25.5, 1.55430712449], [-25.5, 25.5],
 89  [-25.3674915789, 25.5]], [[-11.803398875, -16.2459848116],
 90  [-19.0983005625, 6.20541401733], [-3.5527136788e-15, 20.0811415886],
 91  [19.0983005625, 6.20541401733], [11.803398875, -16.2459848116]],
 92  [[7.45841874701, 25.5], [0, 20.0811415886], [-7.45841874701, 25.5]],
 93  [[25.3674915789, 25.5], [7.45841874701, 25.5], [30.9016994375,
 94  42.5325404176]], [[25.5, 1.55430712449], [19.0983005625,
 95  6.20541401733], [25.3674915789, 25.5], [25.5, 25.5]], [[25.5,
 96  -16.2459848116], [25.5, 1.55430712449], [50, -16.2459848116]],
 97  [[8.79658707105, -25.5], [11.803398875, -16.2459848116], [25.5,
 98  -16.2459848116], [25.5, -25.5]], [[-8.79658707105, -25.5],
 99  [8.79658707105, -25.5], [0, -52.5731112119]], [[-25.5,
100  -16.2459848116], [-11.803398875, -16.2459848116], [-8.79658707105,
101  -25.5], [-25.5, -25.5]]];
102  assert(are_regions_equal(exclusive_or(p3,square(51,center=true)),star_square,either_winding=true));
103  assert(are_regions_equal(exclusive_or(square(51,center=true),p3),star_square,either_winding=true));
104
105}
106test_exclusive_or();
107
108
109
110module test_point_in_region(){
111   region = [for(i=[2:8]) hexagon(r=i)];
112   pir=[for(x=[-6:6],y=[-6:6]) point_in_region([x,y],region)];
113   assert_equal(pir,
114          [-1, -1, -1, 1, 1, -1, 0, -1, 1, 1, -1, -1, -1, -1, 1, 1,
115          -1, -1, 1, 0, 1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 0,
116          -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 0, 1, -1, -1, 1,
117          1, -1, -1, 1, -1, 1, 1, -1, 0, -1, 1, 1, -1, 1, -1, -1, 1,
118          -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, 1, -1, 1,
119          1, 1, -1, 1, -1, 1, -1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1,
120          -1, 1, -1, -1, 1, -1, 1, 1, -1, 0, -1, 1, 1, -1, 1, -1, -1,
121          1, 1, -1, -1, 1, 0, 1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1,
122          -1, 0, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 0, 1, -1,
123          -1, 1, 1, -1, -1, -1, -1, 1, 1, -1, 0, -1, 1, 1, -1, -1, -1]);
124}
125test_point_in_region();
126
127
128module test_make_region(){
129   pentagram = turtle(["move",100,"left",144], repeat=4);
130   region1 = make_region(pentagram);
131   assert_approx(region1, 
132            [[[0, 0], [38.196601125, 0], [30.9016994375, 22.451398829]], [[50,
133            36.3271264003], [19.0983005625, 58.7785252292], [30.9016994375,
134            22.451398829]], [[69.0983005625, 22.451398829], [50, 36.3271264003],
135            [80.9016994375, 58.7785252292]], [[61.803398875, 3.5527136788e-15],
136            [69.0983005625, 22.451398829], [100, 0]], [[38.196601125, 0],
137            [61.803398875, 3.94430452611e-31], [50, -36.3271264003]]]);
138   region2 = make_region(pentagram,nonzero=true);
139   assert_approx(region2,
140            [[[0, 0], [38.196601125, 0], [50, -36.3271264003],
141            [61.803398875, 3.5527136788e-15], [100, 0],
142            [69.0983005625, 22.451398829], [80.9016994375,
143            58.7785252292], [50, 36.3271264003], [19.0983005625,
144            58.7785252292], [30.9016994375, 22.451398829]]]);
145   region3 = make_region([square(10), move([5,5],square(8))]);
146   assert_equal(region3, [[[10, 0], [0, 0], [0, 10], [5, 10], [5, 5], [10, 5]], [[5, 10], [10, 10], [10, 5], [13, 5], [13, 13], [5, 13]]]);
147}
148test_make_region();
149
150
151
152module test_region_area(){
153  assert_equal(region_area([square(10), right(20,square(8))]), 164);
154}
155test_region_area();
156
157